//
//  YLMineHeader.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/31.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLMineHeader.h"
#import <Masonry.h>
#import "YLGlobeHeader.h"
#import "YLMyInfoModel.h"
#import "YLAccountTool.h"
@interface YLMineHeader ()
@property (weak, nonatomic) IBOutlet UIButton *seeButton;

@property (weak, nonatomic) IBOutlet UIView *workerView;


/**
 在投金额
 */
@property (weak, nonatomic) IBOutlet UILabel *amountInvestedLabel;

/**
 预期收益
 */
@property (weak, nonatomic) IBOutlet UILabel *expectedReturnLabel;

/**
 到账收益
 */
@property (weak, nonatomic) IBOutlet UILabel *revenueOnArrivalLabel;

@property (weak, nonatomic) IBOutlet UIImageView *icon;
// 显示说明
@property (weak, nonatomic) IBOutlet UIImageView *showExplainImageView;

@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;

@end

@implementation YLMineHeader

- (void)awakeFromNib
{
    [super awakeFromNib];
    _amountInvestedLabel.text = _revenueOnArrivalLabel.text = _expectedReturnLabel.text = @"0.00";
    
    _iconTap = [[UITapGestureRecognizer alloc] init];
    [_icon addGestureRecognizer:_iconTap];
    
    UITapGestureRecognizer *showExplainTap = [[UITapGestureRecognizer alloc] init];
    _showExplainSignal = [showExplainTap rac_gestureSignal];
    [_showExplainImageView addGestureRecognizer:showExplainTap];
    
    _amountInvestedSubject = [RACSubject subject];
    _expectedReturnSubject = [RACSubject subject];
    _revenueOnArrivalSubject = [RACSubject subject];
    _monthlyBillSubject = [RACSubject subject];

    _workerView.layer.cornerRadius = 6.0f;

    _workerView.layer.shadowOpacity = 0.7f;
    _workerView.layer.shadowColor = [UIColor grayColor].CGColor;
    _workerView.layer.shadowRadius = 8;
    _workerView.layer.shadowOffset = CGSizeMake(8, 8);
    
    
    UITapGestureRecognizer *callTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(callPhone)];
    _workerView.userInteractionEnabled = YES;
    [_workerView addGestureRecognizer:callTap];
    
    
    YLAccount *account = [YLAccountTool account];
    if (account) {
        _workerNameLabel.text = account.workerName;
        _workerNumLabel.text = account.workerNum;
    }

}

- (void)callPhone
{
    NSString *phoneNumber;
    if (_model.workerPhone) {
        phoneNumber = [NSString stringWithFormat:@"tel://%@", _model.workerPhone];
    } else {
        phoneNumber = [NSString stringWithFormat:@"tel://%@", [YLAccountTool account].workerPhone];
    }
    
    
    
    
    if (@available(iOS 11.0, *)) {
       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
    } else {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
         });
    }
    
    
    
}


- (void)setModel:(YLMyInfoModel *)model
{
    _model = model;
    
    _workerNumLabel.text = model.workerNum;
    _workerNameLabel.text = model.workerName;
    
    _phoneLabel.text = model.tel;
    
    _amountInvestedLabel.text = [NSString stringWithFormat:@"%.2f", _model.totalInvestingAmount];
    
    _expectedReturnLabel.text = [NSString stringWithFormat:@"%.2f", _model.expectAmount];
    
    _revenueOnArrivalLabel.text = [NSString stringWithFormat:@"%.2f", _model.cumulativeAmount];
    if (_seeButton.selected) {
        _amountInvestedLabel.text = @"****";
        _expectedReturnLabel.text = _revenueOnArrivalLabel.text = @"***";
    }
    
    
    
   
    
   
    
}

- (IBAction)canSee:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    if (sender.selected) {
        _amountInvestedLabel.text = @"****";
        _expectedReturnLabel.text = _revenueOnArrivalLabel.text = @"***";
        return;
    }
    _amountInvestedLabel.text = [NSString stringWithFormat:@"%.2f", _model.totalInvestingAmount];
    
    _expectedReturnLabel.text = [NSString stringWithFormat:@"%.2f", _model.expectAmount];
    
    _revenueOnArrivalLabel.text = [NSString stringWithFormat:@"%.2f", _model.cumulativeAmount];
}

// 月账单
- (IBAction)monthlyBill:(UIButton *)sender {
    [_monthlyBillSubject sendNext:nil];
}

- (void)setUpUI
{
    __weak typeof(self) weakSelf = self;
    UIImageView *backgroundImageView = [[UIImageView alloc] init];
    backgroundImageView.image = [UIImage imageNamed:@"mineHeader"];
    [self addSubview:backgroundImageView];
    [backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.bottom.mas_equalTo(weakSelf);
    }];
}

// 在投金额
- (IBAction)amountInvested:(UIButton *)sender {
    [_amountInvestedSubject sendNext:nil];
}
// 预期收益
- (IBAction)expectedReturn:(UIButton *)sender {
    [_expectedReturnSubject sendNext:nil];
}
// 到账收益
- (IBAction)revenueOnArrival:(UIButton *)sender {
    [_revenueOnArrivalSubject sendNext:nil];
}


@end
